﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;
using UniRlv.Utility.Extend;
using System.Linq;

namespace UniRlv.UI.Main.DataManage
{
    public partial class FrmCodeData : UniRlv.UI.Main.FrmBase
    {
        public FrmCodeData()
        {
            InitializeComponent();
        }

        private void FrmCodeData_Load(object sender, EventArgs e)
        {
            lstCdt.SelectedIndex = 0;

            toolBtnQuery.Visible = true;
            toolBtnQuery.Click += toolBtnQuery_Click;

            UIHelper.InitDataGridView(dgvDetails);

            dgvDetails.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void toolBtnQuery_Click(object sender, EventArgs e)
        {
            string code = txtCode.Text;
            if (!string.IsNullOrEmpty(code))
            {
                QueryCode(code);
            }
        }

        public override void OnScanCode(string orgCode, string validCode)
        {
            txtCode.Text = validCode;
            QueryCode(validCode);
        }

        private void QueryCode(string validCode)
        {
            string sql = EntityHelper.GetQuerySql<CodeInfo>();
            sql += string.Format(" where code='{0}'", validCode);
            string msg = string.Empty;
            CodeInfo ci;

            this.Cursor = Cursors.WaitCursor;

            if (chkArchieve.Checked)
            {
                ci = DALOpr.QuerySingleArchieve<CodeInfo>(sql, ref msg);
            }
            else
            {
                ci = DALOpr.QuerySingle<CodeInfo>(sql, ref msg);
            }
            
            List<CodeInfo> codes;//所有码合集
            //List<CodeInfo> codes2;//二级码合集

            
            if (ci!=null)
            {
                sql = EntityHelper.GetQuerySql<ProductionTask>();
                sql += string.Format(" where id={0}", ci.taskID);
                msg = string.Empty;
                ProductionTask t;

                if (chkArchieve.Checked)
                {
                    t = DALOpr.QuerySingleArchieve<ProductionTask>(sql, ref msg);
                }
                else
                {
                    t = DALOpr.QuerySingle<ProductionTask>(sql, ref msg);
                }
                

                if (t!=null)
                {
                    try
                    {
                        sql = string.Format("taskID={0}", t.ID);

                        msg = string.Empty;
                        codes = DALOpr.Query<CodeInfo>(sql, ref msg);

                        Dictionary<string, object> dic = EntityHelper.GetDicData(ci);
                        List<Dictionary<string, object>> ld = new List<Dictionary<string, object>>();
                        DataTable dt;

                        dic.Add("batch", t.batch);
                        dic.Add("tym", t.tym);
                        dic.Add("pzwh", t.pzwh);
                        dic.Add("ratio", t.tagRatio);
                        int ratio;

                        if (codes != null && codes.Count > 0)
                        {
                            if (t.tagLevel == 1)
                            {
                                dic.Add("OutsideCodeSN", ci.serialNumber);

                                dgvDetails.Columns["OutsideCodeSN2"].Visible = false;
                                dgvDetails.Columns["OutsideCodeSN3"].Visible = false;
                                dgvDetails.Columns["OutsideCodeSN4"].Visible = false;

                                dgvDetails.Columns["OutsideCode2"].Visible = false;
                                dgvDetails.Columns["OutsideCode3"].Visible = false;
                                dgvDetails.Columns["OutsideCode4"].Visible = false;
                            }
                            else if (t.tagLevel == 2)
                            {

                                ratio = AppHelper.GetStationRatio2(ci.stationNumber, t.tagRatio);//r22/r21

                                dic.Add("OutsideCodeSN2", (ci.serialNumber - 1) / ratio + 1);
                                dic.Add("OutsideCode2", codes.Find(s => s.stationNumber == 2 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);

                                dgvDetails.Columns["OutsideCodeSN3"].Visible = false;
                                dgvDetails.Columns["OutsideCodeSN4"].Visible = false;

                                dgvDetails.Columns["OutsideCode3"].Visible = false;
                                dgvDetails.Columns["OutsideCode4"].Visible = false;

                                if (ci.stationNumber == 2)
                                {
                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = false;
                                    dgvDetails.Columns["OutsideCode2"].Visible = false;
                                }
                                else
                                {
                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = true;
                                    dgvDetails.Columns["OutsideCode2"].Visible = true;

                                }
                            }
                            else if (t.tagLevel == 3)
                            {
                                dgvDetails.Columns["OutsideCodeSN4"].Visible = false;
                                dgvDetails.Columns["OutsideCode4"].Visible = false;


                                if (ci.stationNumber == 1)
                                {
                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = true;
                                    dgvDetails.Columns["OutsideCodeSN3"].Visible = true;

                                    dgvDetails.Columns["OutsideCode2"].Visible = true;
                                    dgvDetails.Columns["OutsideCode3"].Visible = true;

                                    ratio = AppHelper.GetStationRatio(2, t.tagRatio);//r21
                                    dic.Add("OutsideCodeSN2", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode2", codes.Find(s => s.stationNumber == 2 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);

                                    ratio = AppHelper.GetStationRatio(3, t.tagRatio);//r31
                                    dic.Add("OutsideCodeSN3", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode3", codes.Find(s => s.stationNumber == 3 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);
                                }
                                else if (ci.stationNumber == 2)
                                {
                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = false;
                                    dgvDetails.Columns["OutsideCodeSN3"].Visible = true;

                                    dgvDetails.Columns["OutsideCode2"].Visible = false;
                                    dgvDetails.Columns["OutsideCode3"].Visible = true;

                                    ratio = AppHelper.GetStationRatio2(ci.stationNumber, t.tagRatio);//r32
                                    dic.Add("OutsideCodeSN3", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode3", codes.Find(s => s.stationNumber == 3 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);
                                }
                                else if (ci.stationNumber == 3)
                                {
                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = false;
                                    dgvDetails.Columns["OutsideCodeSN3"].Visible = false;

                                    dgvDetails.Columns["OutsideCode2"].Visible = false;
                                    dgvDetails.Columns["OutsideCode3"].Visible = false;
                                }
                            }
                            else
                            {
                                if (ci.stationNumber == 1)
                                {
                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = true;
                                    dgvDetails.Columns["OutsideCodeSN3"].Visible = true;
                                    dgvDetails.Columns["OutsideCodeSN4"].Visible = true;

                                    dgvDetails.Columns["OutsideCode2"].Visible = true;
                                    dgvDetails.Columns["OutsideCode3"].Visible = true;
                                    dgvDetails.Columns["OutsideCode4"].Visible = true;

                                    ratio = AppHelper.GetStationRatio(2, t.tagRatio);//r21
                                    dic.Add("OutsideCodeSN2", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode2", codes.Find(s => s.stationNumber == 2 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);

                                    ratio = AppHelper.GetStationRatio(3, t.tagRatio);//r31
                                    dic.Add("OutsideCodeSN3", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode3", codes.Find(s => s.stationNumber == 3 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);

                                    ratio = AppHelper.GetStationRatio(4, t.tagRatio);//r41
                                    dic.Add("OutsideCodeSN4", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode4", codes.Find(s => s.stationNumber == 4 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);
                                }
                                else if (ci.stationNumber == 2)
                                {
                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = false;
                                    dgvDetails.Columns["OutsideCodeSN3"].Visible = true;
                                    dgvDetails.Columns["OutsideCodeSN4"].Visible = true;

                                    dgvDetails.Columns["OutsideCode2"].Visible = false;
                                    dgvDetails.Columns["OutsideCode3"].Visible = true;
                                    dgvDetails.Columns["OutsideCode4"].Visible = true;

                                    int[] ratios = AppHelper.GetLevelRatios(t.tagRatio);

                                    ratio = ratios[1];//r32
                                    dic.Add("OutsideCodeSN3", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode3", codes.Find(s => s.stationNumber == 3 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);

                                    ratio = ratios[1] * ratios[0];//r42
                                    dic.Add("OutsideCodeSN4", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode4", codes.Find(s => s.stationNumber == 4 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);
                                }
                                else if (ci.stationNumber == 3)
                                {
                                    ratio = AppHelper.GetStationRatio2(ci.stationNumber, t.tagRatio);//r43
                                    dic.Add("OutsideCodeSN4", (ci.serialNumber - 1) / ratio + 1);
                                    dic.Add("OutsideCode4", codes.Find(s => s.stationNumber == 4 && s.serialNumber == (ci.serialNumber - 1) / ratio + 1).code);

                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = false;
                                    dgvDetails.Columns["OutsideCodeSN3"].Visible = false;
                                    dgvDetails.Columns["OutsideCodeSN4"].Visible = true;

                                    dgvDetails.Columns["OutsideCode2"].Visible = false;
                                    dgvDetails.Columns["OutsideCode3"].Visible = false;
                                    dgvDetails.Columns["OutsideCode4"].Visible = true;
                                }
                                else
                                {
                                    dgvDetails.Columns["OutsideCodeSN2"].Visible = false;
                                    dgvDetails.Columns["OutsideCodeSN3"].Visible = false;
                                    dgvDetails.Columns["OutsideCodeSN4"].Visible = false;

                                    dgvDetails.Columns["OutsideCode2"].Visible = false;
                                    dgvDetails.Columns["OutsideCode3"].Visible = false;
                                    dgvDetails.Columns["OutsideCode4"].Visible = false;
                                }
                            }
                        }
                        else
                        {
                            dgvDetails.Columns["OutsideCodeSN1"].Visible = false;
                            dgvDetails.Columns["OutsideCodeSN2"].Visible = false;
                            dgvDetails.Columns["OutsideCodeSN3"].Visible = false;
                            dgvDetails.Columns["OutsideCodeSN4"].Visible = false;

                            dgvDetails.Columns["OutsideCode1"].Visible = false;
                            dgvDetails.Columns["OutsideCode2"].Visible = false;
                            dgvDetails.Columns["OutsideCode3"].Visible = false;
                            dgvDetails.Columns["OutsideCode4"].Visible = false;

                            MessageBox.Show("码存在，但是无法找到条码所属生产任务的其他条码信息");
                        }
                            
                        ld.Add(dic);
                        dt = ld.ToDataTable();
                        dt.TableName = "CodeInfo";

                        dgvDetails.DataSource = dt;
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show("查询失败：" + exp.ToString());
                    }
                    
                }
                else
                {

                    ShowInfoMessageBox("提示", $"码存在，{ci.stationNumber}级码，入库时间{ci.inTime}，但是所属生产任务不存在，查询不到任务记录");
                }
            }
            else
            {
                ShowInfoMessageBox("提示", "码不存在，查询不到码记录");
            }

            this.Cursor = Cursors.Default;
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtCode.Focused && e.KeyCode == Keys.Enter)
            {
                string code = txtCode.Text;
                if (!string.IsNullOrEmpty(code))
                {
                    QueryCode(code);
                }
            }
        }

        private void menuCopyCOntent_Click(object sender, EventArgs e)
        {

            if (dgvDetails.CurrentCell!=null)
            {
                Clipboard.SetText(dgvDetails.CurrentCell.Value.ToString());
            }
        }

        private void dgvDetails_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dgvDetails.ClearSelection();
                    dgvDetails.Rows[e.RowIndex].Selected = true;
                    dgvDetails.CurrentCell = dgvDetails.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }
    }
}
